package cn.xfdzx.android.apps.shop.fragment.market;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.adapter.XFDShopAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.ClassShopBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ClassShopContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ClassProductPresenter;
import cn.xfdzx.android.apps.shop.presenter.ClassShopPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：新发地市场采购
 */
public class ShopFragment extends BaseMvpFragment<ClassShopPresenter> implements ClassShopContract.View {

    RecyclerView rvShop;
    SmartRefreshLayout mRefreshLayout;

    private int type = 1;//类别 0-零售 1-批发
    private int pages=1;
    private int limit = Constants.LIMIT;//每页多少条
    private String sid = "";
    private String keyWrod = "";

    private LinearLayout ll_hint;

    private List<ClassShopBeen.DataBean> mList = new ArrayList<>();


    private XFDShopAdapter xfdShopAdapter;

    @Override
    protected void initView(View view) {
        rvShop=view.findViewById(R.id.rv_shop);
        mRefreshLayout=view.findViewById(R.id.refreshLayout);
        ll_hint=view.findViewById(R.id.ll_hint);

        mPresenter = new ClassShopPresenter();
        mPresenter.attachView(this);
        sid= getArguments().getString("id");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvShop.setLayoutManager(linearLayoutManager);
        xfdShopAdapter = new XFDShopAdapter(getContext(),mList);
        xfdShopAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent=new Intent(getActivity(), XFDShopDetailsActivity.class);
                intent.putExtra("shop_id",mList.get(position).getId()+"");
                 startActivity(intent);
            }
        });
        rvShop.setAdapter(xfdShopAdapter);

        mPresenter.initData(true, false, sid, pages + "", limit + "", type + "", "1", "", "", "", "", "");
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                pages = 1;
                mPresenter.initData(true, true, sid, pages + "", limit + "", type + "", "1", "", "", "", "", "");

            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(getActivity());
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }


    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSuccess(ClassShopBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (pages == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initData(false, false, sid, pages + "", limit + "", type + "", "1", "", "", "", "", "");
                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                pages++;
            }
            mList.addAll(bean.getData());
            if (mList.size()==0){
                ll_hint.setVisibility(View.VISIBLE);
            }else {
                ll_hint.setVisibility(View.GONE);
            }
            xfdShopAdapter.notifyDataSetChanged();
        }else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    public static ShopFragment newInstance(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
//        bundle.putLong(Constants.UID, userUid);
        ShopFragment fragment = new ShopFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onDestroy() {
        mList.clear();
        mList=null;
        super.onDestroy();
    }

}
